package com.projectx.adpter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.action.chat.SingleChatActivity;
import com.projectx.action.chat.SwitchChat;
import com.projectx.entity.LastChatRecord;

import java.util.List;

public class ChatRecordAdapter extends RecyclerView.Adapter<ChatRecordAdapter.VH> {
    private Context context;
    private List<LastChatRecord> lastChatRecords;
    private String username;

    public ChatRecordAdapter(Context context,List<LastChatRecord> lastChatRecords,String username){
        this.context = context;
        this.lastChatRecords = lastChatRecords;
        this.username = username;
    }
    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_current_chat,parent,false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH holder, final int position) {
        holder.friendAccount.setText(lastChatRecords.get(position).getAccountName());
        holder.lastRecord.setText(lastChatRecords.get(position).getLastRecord());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SwitchChat.startChat(context,username,holder.friendAccount.getText().toString(),true);
            }
        });
        holder.view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final PopupWindow popupWindow = new PopupWindow(context);
                popupWindow.setBackgroundDrawable(context.getDrawable(R.drawable.bg_shadow_white));
                View view = LayoutInflater.from(context).inflate(R.layout.item_menu_chat_record,null,false);
                view.findViewById(R.id.delete_data).setOnClickListener(new View.OnClickListener() {//设置删除监听
                    @Override
                    public void onClick(View v) {
                        lastChatRecords.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position,getItemCount());
                        popupWindow.dismiss();
                    }
                });
                popupWindow.setContentView(view);
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(v,0,0);
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return lastChatRecords.size();
    }

    public static class VH extends RecyclerView.ViewHolder {

        View view;
        TextView friendAccount;
        TextView lastRecord;
        TextView lastTime;


        ImageView headImage;

        public VH(@NonNull View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.ItemCurrentChat);
            friendAccount = itemView.findViewById(R.id.FriendAccount);
            lastRecord = itemView.findViewById(R.id.LastRecord);
            headImage = itemView.findViewById(R.id.HeadImage);
            lastTime = itemView.findViewById(R.id.LastRecordTime);
        }
    }
}
